/*
 * Copyright (c) 2009-2013 EvTech Project
 * All rights reserved.
 * 
 * This file is part of LModeller.
 *
 * LModeller is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or 
 * (at your option) any later version.
 *
 * LModeller is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with LModeller.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */
package fi.honkalampisaatio.lmodeler.setup;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import fi.honkalampisaatio.game.utilities.MediaLoader;

public class ImagePanel extends JPanel {
	private static final long serialVersionUID = 6507455212219062470L;

	private BufferedImage img;
	private String imgPath;
	
	public void setImage(String imgPath)
	{
		this.imgPath = imgPath;
		this.img = MediaLoader.createImage(imgPath, 600, 600);
		if(this.img==null)
			this.imgPath = "";
		repaint();
	}
	
	public String getImage()
	{
		return imgPath;
	}

	public void paintComponent(Graphics g) {
        super.paintComponent(g);  
        Graphics2D g2 = (Graphics2D) g;
        if(img!=null)
        {
			int imgHeight = (int) (getHeight()*0.9);
			int imgWidth = (int)(imgHeight * img.getWidth() / img.getHeight());
			if(imgWidth>getWidth() * 0.9)
			{
				imgWidth = (int)(getWidth() * 0.9);
				imgHeight = (int)(img.getHeight() * imgWidth / img.getWidth());
			}
			g2.drawImage(img, (getWidth()-imgWidth)/2, 
					(getHeight()-imgHeight)/2, imgWidth-1, imgHeight-1, null);
        }
		g2.drawRect(0, 0, this.getWidth()-1, this.getHeight()-1);
	}
}
